package no.rodland.acc.main;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import no.rodland.acc.action.UploadAction;
import no.rodland.acc.db.StoreService;
import no.rodland.acc.parsers.BudgetParser;
import no.rodland.acc.parsers.DateParser;
import no.rodland.acc.parsers.DbRowParser;
import no.rodland.acc.parsers.ExcelBankParser;
import no.rodland.acc.parsers.NumberParser;
import no.rodland.acc.parsers.SkandiaBankParser;
import no.rodland.acc.parsers.TextDateParser;
import no.rodland.acc.service.DbServiceImpl;
import no.rodland.acc.db.FetchService;
import no.rodland.acc.utils.Config;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jul 5, 2010 12:05:52 PM
 * @Author: fmr
 */
@SuppressWarnings({"FieldCanBeLocal"})
public class Uploader {
    private static final Logger log = Logger.getLogger(Uploader.class);

    private static BasicDataSource ds;
    private static SkandiaBankParser sbParser;
    private static ExcelBankParser excelParser;
    private static BudgetParser oldBudgetParser;
    private static BudgetParser newBudgetParser;
    private static boolean reallyRun = true;

    public static void main(String[] args) {
        DbServiceImpl fetchService = new DbServiceImpl(getDataSource());
        //StoreService storeService = new StoreServiceImpl(getDataSource(), fetchService, fetchService.getRowMappers());
        UploadAction action = new UploadAction(getParsers(fetchService, fetchService), fetchService);

        for (String arg : args) {
            parseFile(action, new File(arg));
        }
    }

    private static void parseFile(UploadAction action, File file) {
        if (file == null) {
            return;
        }
        if (file.isDirectory()) {
            for (File file1 : file.listFiles()) {
                parseFile(action, file1);
            }
        }
        if (!file.getName().contains(".csv")) {
            log.error("only csv-files may be parsed: skipping " + file.getName());
            return;
        }
        DbRowParser parser = getParser(file);
        if (parser != null){
        action.setParser(parser.getName());
        action.setUpload(file);
        log.info("file = " + file.getName() + ", parser = " + parser);
        log.info(reallyRun ? action.execute() : "Would've imported " + file.getName());
        }
        else{
            log.warn("No parser found for " + file.getName());
        }
    }

    private static DbRowParser getParser(File file) {
        String fileName = file.getName();
        if (fileName.contains("9710")) {
            return sbParser;
        }
        else if (fileName.contains("konto")) {
            return null;
        }
        else if (fileName.contains("forlsag")) {
            return null;
        }
        else if (fileName.contains("forslag")) {
            return newBudgetParser;
        }
        else if (fileName.contains("2003") | fileName.contains("2004")) {
            return oldBudgetParser;
        }
        else if (fileName.contains("200")) {
            return newBudgetParser;
        }
        return null;
    }

    private static BasicDataSource getDataSource() {
        if (ds == null) {
            ds = new BasicDataSource();
            ds.setPassword(Config.getProperty("db.password"));
            ds.setUrl(Config.getProperty("db.url"));
            ds.setUsername(Config.getProperty("db.username"));
            ds.setDriverClassName(Config.getProperty("db.driver"));
        }
        return ds;
    }

    private static Set<DbRowParser> getParsers(FetchService readService, StoreService storeService) {
        Set<DbRowParser> set = new HashSet<DbRowParser>();
        DateParser dateParser = new DateParser();
        NumberParser numberParser = new NumberParser();
        TextDateParser textDateParser = new TextDateParser();
        Config.on(dateParser);
        Config.on(numberParser);
        Config.on(textDateParser);

        sbParser = new SkandiaBankParser(readService,
                storeService,
                dateParser,
                numberParser,
                textDateParser);
        excelParser = new ExcelBankParser(readService,
                storeService,
                dateParser,
                numberParser,
                textDateParser);
        oldBudgetParser = new BudgetParser(readService, textDateParser, dateParser, numberParser, true);
        newBudgetParser = new BudgetParser(readService, textDateParser, dateParser, numberParser, false);

        Config.on(sbParser);
        Config.on(excelParser);
        Config.on(oldBudgetParser);
        Config.on(newBudgetParser);

        set.add(sbParser);
        set.add(excelParser);
        set.add(oldBudgetParser);
        set.add(newBudgetParser);
        return set;
    }
}
